import $axios from "@/network";
import {orgList} from "@/api/operations-center/org";
import {roleList} from "@/api/operations-center/role";

/**
 * {用户公共数据}
 *
 * @author: 宋康
 * @date: 2022/8/22 14:15
 */
export const userPublic = {
  data() {
    const validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.form.checkPass !== '') {
          this.$refs.form.validateField('checkPass');
        }
        callback();
      }
    };
    const validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.form.pass) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      tableData: [],
      tableData2: [],
      form: {
        pass: '',
        checkPass: '',
        loginName: '',
        loginPwd: '',
        userName: '',
        realName: '',
        phone: '',
        email: '',
        roles: [],
        organizations: []
      },
      rules: {
        loginName: [
          {required: true, message: '请输入登录账号', trigger: 'blur'},
          {min: 1, max: 100, message: '长度在 1 到 100 个字符', trigger: 'blur'}
        ],
        userName: [
          {required: true, message: '请输入用户名称', trigger: 'blur'},
          {min: 1, max: 100, message: '长度在 1 到 100 个字符', trigger: 'blur'}
        ],
        pass: [
          {validator: validatePass, trigger: 'blur'},
          {required: true, message: '请输入登录密码', trigger: 'blur'},
          {min: 6, max: 18, message: '长度在 6 到 18 个字符', trigger: 'blur'}
        ],
        checkPass: [
          {validator: validatePass2, trigger: 'blur'},
          {required: true, message: '请输入确认新密码', trigger: 'blur'},
          {min: 6, max: 18, message: '长度在 6 到 18 个字符', trigger: 'blur'}
        ],
      }
    }
  },
  methods: {
    /**
     * {方法功能中文描述}
     *
     * @author: 宋康
     * @date: 2022/8/22 14:17
     */
    cancel() {
      this.$router.go(-1)
    },
    /**
     * {获取单选}
     *
     * @author: 宋康
     * @date: 2022/8/22 16:14
     */
    getRadioRecord(tableName) {
      let id = '0'
      let parent = this.$refs[tableName].getRadioRecord();
      if (parent) {
        id = parent.id;
      }
      return id;
    },
    /**
     * {获取多选}
     *
     * @author: 宋康
     * @date: 2022/8/22 16:14
     */
    getCheckboxRecords(tableName) {
      let checkboxData = [];
      // 全选
      let entirely = this.$refs[tableName].getCheckboxRecords();
      if (entirely.length > 0) {
        for (let p of entirely) {
          checkboxData.push(p.id);
        }
        // 半选
        let Indeterminate = this.$refs[tableName].getCheckboxIndeterminateRecords();
        if (Indeterminate.length > 0) {
          for (let p of Indeterminate) {
            checkboxData.push(p.id);
          }
        }
      }
      return checkboxData;
    },
    /**
     * {查询组织机构列表}
     *
     * @author: 宋康
     * @date: 2022/8/22 13:39
     */
    queryOrgList() {
      orgList('', false, []).then(res => {
        this.tableData = res.data;
      })
    },
    /**
     * {获取角色列表}
     *
     * @author: 宋康
     * @date: 2022/8/22 15:58
     */
    queryRoleList() {
      roleList('', false).then(res => {
        if (res.status) {
          this.tableData2 = res.data;
        }
      })
    },
  }
}
/**
 * {获取用户分页}
 *
 * @author: 宋康
 * @date: 2022/8/22 13:47
 */
export const userPage = (current, size, keyword, organizations) => {
  return $axios({
    method: 'POST',
    url: '/user/query/page',
    params: {
      current,
      size
    },
    data: {
      keyword,
      organizations
    }
  })
}

/**
 * {查询指定用户}
 * @param uid
 * @author: 宋康
 * @date: 2022/8/23 09:03
 */
export const queryUserById = (id) => {
  return $axios({
    url: '/user/query/o',
    params: {
      id
    }
  })
}

/**
 * {保存用户}
 * @param option
 * @author: 宋康
 * @date: 2022/8/22 16:23
 */
export const saveUser = (option) => {
  return $axios({
    method: 'POST',
    url: '/user/save',
    data: {
      ...option
    }
  })
}

/**
 * {编辑用户}
 * @param option
 * @author: 宋康
 * @date: 2022/8/23 10:16
 */
export const editUser = (option) => {
  return $axios({
    method: 'POST',
    url: '/user/edit',
    data: {
      ...option
    }
  })
}


/**
 * {移除指定用户}
 * @param uid
 * @author: 宋康
 * @date: 2022/8/23 10:33
 */
export const removeUser = (uid) => {
  return $axios({
    method: 'POST',
    url: '/user/remove',
    params: {
      uid
    }
  })
}



